package org.example.com.pattern.create.builder

/**
 * 创建型模式-建造者模式
 * 适用于需要分步骤创建复杂对象的场景
 */

class Car

class Manual

interface Engine
class SportEngine : Engine

interface Builder {
    fun reset()
    fun setSeats(number: Int)
    fun setEngine(engine: Engine)
    fun setTripComputer(flag: Boolean)
    fun setGPS(flag: Boolean)
}

/**
 * 汽车配置
 * 可能具有不同的配置
 */
class CarBuilder() : Builder {
    private var car: Car? = null

    init {
        reset()
    }

    override fun reset() {
        car = Car()
    }

    override fun setSeats(number: Int) {
    }

    override fun setEngine(engine: Engine) {
    }

    override fun setTripComputer(flag: Boolean) {
    }

    override fun setGPS(flag: Boolean) {
    }

    fun getProduct(): Car {
        val product = this.car
        this.reset()
        return product!!
    }
}

/**
 * 用户手册
 * 应该根据汽车配置进行编制，并介绍汽车所有功能
 */
class CarManualBuilder : Builder {
    private var manual: Manual? = null

    init {
        reset()
    }

    override fun reset() {
        manual = Manual()
    }

    override fun setSeats(number: Int) {
    }

    override fun setEngine(engine: Engine) {
    }

    override fun setTripComputer(flag: Boolean) {
    }

    override fun setGPS(flag: Boolean) {
    }

    fun getProduct(): Manual {
        val product = manual
        reset()
        return product!!
    }
}

class Director {
    fun constructSpotCar(builder: Builder) {
        builder.reset()
        builder.setSeats(2)
        builder.setEngine(SportEngine())
        builder.setTripComputer(true)
        builder.setGPS(true)
    }

    fun constructSUV(builder: Builder) {

    }
}

class Application {
    fun makeCar(){
        val director = Director()

        val carBuilder = CarBuilder()
        director.constructSpotCar(carBuilder)
        val car = carBuilder.getProduct()

        val carManualBuilder = CarManualBuilder()
        director.constructSpotCar(carBuilder)
        val manual = carManualBuilder.getProduct()

    }
}
